﻿using Microsoft.EntityFrameworkCore;
using NetCoreBBS.Entities;
using NetCoreBBS.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace NetCoreBBS.Infrastructure.Repositorys
{
    public class CustomerRepository : Repository<Customer>, ICustomerRepository
    {
        private readonly DataContext _dbContext;

        public CustomerRepository(DataContext dbContext)
           : base(dbContext)
        {
            _dbContext = dbContext;
        }

        public Page<Customer> PageList(Expression<Func<Customer, bool>> predicate, int pagesize, int pageindex)
        {
            var customers = _dbContext.Customers.AsQueryable().AsNoTracking();
            if (predicate != null)
            {
                customers = customers.Where(predicate);
            }
            var count = customers.Count();
            customers = customers.OrderByDescending(r => r.ArrivedTime)
                    .Skip((pageindex - 1) * pagesize).Take(pagesize);
            return new Page<Customer>(customers.ToList(), pagesize, count);
        }

        public Page<Customer> PageList(int pagesize = 20, int pageindex = 1)
        {
            return PageList(null, pagesize, pageindex);
        }
    }
}
